<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\SystemWebsite;
use app\admin\model\website\Banner;
use app\admin\model\WebsiteDefailt;
use app\common\controller\AdminController;
use app\common\service\MenuService;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use broadcast\redis\setRedis;
use app\models\system\Rooms;

use think\facade\Cache;
/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="站点管理")
 */
class System extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemWebsite();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index ()
    {
        $website = session("website");
        $count = array();
        
        //判断是否有缓存
        $banner = setRedis::getCache($website,'bannList');
        if (!$banner) {
            $banner = Banner::select()->toArray();
        }
        for ($i=count($banner);$i<4;$i++) {
            $count[$i] = 0;
        }


        $system = setRedis::getCache($website,'info');
        if (!$system) {
            $system = SystemWebsite::where("website_ident",$website)->find();
            if($system) $system = $system->toArray();
        }

        // 课程安排模式
        if (!isset($system['curriculum_arrange_pattern'])) {
            $system['curriculum_arrange_pattern'] = 1;
        }

        if (!isset($system['lecturer_arrange_pattern'])) {
            $system['lecturer_arrange_pattern'] = 1;
        }



        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $time = time();

            if (!isset($post['website_state'])) $post['website_state'] = 0;
            if (!isset($post['is_register'])) $post['is_register'] = 0;
            if (!isset($post['message_audit'])) $post['message_audit'] = 0;
            if (!isset($post['visitor_login'])) $post['visitor_login'] = 0;


                //banner 处理
                Db::connect(session("website"))->execute("TRUNCATE table chat_banner");
                
                $bannerList = array();
                for ($i=0;$i<4;$i++) {
                    if ($post['banner'.($i+1)] != "") {
                        $bannerList[] = ['image'=>$post['banner'.($i+1)],'create_time'=>$time];
                    }
                    unset($post['banner'.($i+1)]);
                }
                
                //获取网站信息 && 判断网站信息是否修改
                $lowWebsiteInfo = setRedis::getCache(session("website"),"info");
                if ($lowWebsiteInfo['website_state'] != $post['website_state']) {
                
                    Rooms::where("id","<>","0")->update(['status'=>$post['website_state']]);
                    setRedis::editRooms(session("website"),'roomInfo',['status'=>$post['website_state']]);
                }

                if ($lowWebsiteInfo['fictitious_base'] != $post['fictitious_base'] || $lowWebsiteInfo['fictitious_upper'] != $post['fictitious_upper'] || $lowWebsiteInfo['fictitious_lower'] != $post['fictitious_lower'] || $lowWebsiteInfo['interval_press'] != $post['interval_press']) {
                   
                    Rooms::where("id","<>","0")->update(['fictitious_base'=>$post['fictitious_base'],'fictitious_top'=>$post['fictitious_upper'],'fictitious_botton'=>$post['fictitious_lower'],'interval_time'=>$post['interval_press']]);
                    setRedis::editRooms(session("website"),'roomInfo',['fictitious_base'=>$post['fictitious_base'],'fictitious_top'=>$post['fictitious_upper'],'fictitious_botton'=>$post['fictitious_lower'],'interval_time'=>$post['interval_press']]);
                }

                if ($lowWebsiteInfo['message_audit'] != $post['message_audit']) {
                    Rooms::where("id","<>","0")->update(['message_audit'=>$post['message_audit']]);
                    setRedis::editRooms(session("website"),'roomInfo',['message_audit'=>$post['message_audit']]);
                }

                if ($lowWebsiteInfo['visitor_login'] != $post['visitor_login']) {
                    Rooms::where("id","<>","0")->update(['visitors_speak'=>$post['visitor_login']]);
                    setRedis::editRooms(session("website"),'roomInfo',['visitors_speak'=>$post['visitor_login']]);
                }

                $post['website_title'] = $post['name'];
                unset($post['name']);
                unset($post['file']);
                
                $post['update_time'] = $time;
                $id = (int)$post['id'];
                unset($post['id']);
                
                
                // Db::connect(session("website"))->table("chat_banner")->insertAll($bannerList);
                $banners = new Banner();
                $banners->saveAll($bannerList);
                
                $save = SystemWebsite::where("id",$id)->save($post);
                //websiteInfo 网站信息
                $websiteInfo = SystemWebsite::where("website_ident",session("website"))->find();
                if($websiteInfo) $websiteInfo = $websiteInfo->toArray();
                if (empty($websiteInfo)) {
                    setRedis::delCache(session("website"),"info");
                }else{
                    setRedis::setCache(session("website"),'info',$websiteInfo,0);
                }

                //banner  添加缓存
                //banner  添加缓存
                $nowBanner = Db::connect(session("website"))
                ->table("chat_banner")->select()->toArray();
                if (empty($nowBanner)) {
                    setRedis::delCache(session("website"),"bannList");
                }else{
                    setRedis::setCache(session("website"),'bannList',$nowBanner,0);
                }
            
            
            $this->success('保存成功');

        }


        //
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList = $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $group = json_decode($groupList['configure'],true);

        if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
            $this->assign("islecturer",0);
        }else{
            $this->assign('islecturer',1);
        }

        if ($group['whether_curriculum'] != 1) {
            $this->assign("iscurriculum",0);
        }else{
            $this->assign('iscurriculum',1);
        }


        $this->assign("count",$count);
        $this->assign("system",$system);
        $this->assign("banner",$banner);
        return $this->fetch();
    }



    

}
